/**
 * UNIVERSIDADE FEDERAL DO RIO GRANDE DO NORTE - UFRN
 * DEPARTAMENTO DE INFORMATICA E MATEMATICA APLICADA - DIMAP
 * Programa de Pos-Graduacao em Sistemas e Computacao - PPGSC
 */
package br.ufrn.spl.ev.gui.managers;

import java.io.File;

import br.ufrn.spl.ev.PluginConstants;
import br.ufrn.spl.ev.engines.conflicts.indirect.IndirectConflictsDetectionStrategy;
import br.ufrn.spl.ev.engines.miners.svn.SVNConnector;
import br.ufrn.spl.ev.gui.swt.ShowEvolutionsUI;
import br.ufrn.spl.ev.models.changeloghistorymodel.ChangeLogHistory;
import br.ufrn.spl.ev.models.conflictmodel.ConflictModel;
import br.ufrn.spl.ev.util.ChangeLogHistoryUtil;
import br.ufrn.spl.ev.util.GUIUtils;
import br.ufrn.spl.ev.util.ProductLineConflictModelUtil;

/**
 *  <p>Executes the process of analyze indirect conflicts</p>
 *
 *  <p>Controls the flow of the tool, call the strategy to make the job, and after call a UI to show the results.</p>
 *
 * @author jadson - jadson@info.ufrn.br
 * @vesion 1.0 - Class Creation.
 * @since 09/10/2013
 *
 */
public class AnalyzeIndirectConflictsManager {

	/* Reference to the main UI of the tool */
	private ShowEvolutionsUI showEvolutionsUI;
	
	public AnalyzeIndirectConflictsManager(ShowEvolutionsUI showEvolutionsUI){
		this.showEvolutionsUI = showEvolutionsUI;
	}
	
	/**
	 * Make the process the execution of indirect conflicts
	 * 
	 * @throws Exception
	 */
	public void executeIndiredConflictsAnalisis(ChangeLogHistory changeLogHistorySource, ChangeLogHistory changeLogHistoryTarget, ConflictModel productLineConflictModel, SVNConnector sourceConnector, SVNConnector targetConnector, IndirectConflictsDetectionStrategy strategy, short depthAnalysisLevel) throws Exception{
		
		long startTime = System.currentTimeMillis();
		
		if(strategy == null){
			GUIUtils.addErrorMessage("An indirect Conflict Strategy need to be chosen !");
			return;
		}
		
		if(changeLogHistorySource == null || changeLogHistoryTarget == null){
			GUIUtils.addErrorMessage("No Information about Evolution was passed ! ");
			return;
		}
		
		System.out.println("/////  STARTIN INDIRECT CONFLICT ANALYSIS "+((System.currentTimeMillis()-startTime)/1000/60)+" minutes //////");
		
		// Mark the indirect conflict on the ChangeLogHistory source and reload the same interface to show it to the user
		strategy.checkIndirectConflicts( changeLogHistorySource,  changeLogHistoryTarget,  sourceConnector, targetConnector, depthAnalysisLevel);
		
		System.out.println("/////  INDIRECT CONFLICT ANALYSIS IN "+((System.currentTimeMillis()-startTime)/1000/60)+" minutes //////");
		
		// save analysis made 
		ChangeLogHistoryUtil.writeHistoryChangeLogFile(changeLogHistorySource, new File(showEvolutionsUI.getWorkDirectoryPath()+PluginConstants.HISTORY_CHANGE_LOG_SOURCE_FILE));
		ChangeLogHistoryUtil.writeHistoryChangeLogFile(changeLogHistoryTarget, new File(showEvolutionsUI.getWorkDirectoryPath()+PluginConstants.HISTORY_CHANGE_LOG_TARGET_FILE));
		ProductLineConflictModelUtil.writeProductLineConflictModelToFile(new File(showEvolutionsUI.getWorkDirectoryPath()+PluginConstants.PRODUCTI_LINE_CONFLICT_MODEL_FILE));
		
		
		saveAndShowEvolutionResults(changeLogHistorySource, changeLogHistoryTarget, null);
		
		// reload the information to the user 
		showEvolutionsUI.showEvolutionsAnalisis(changeLogHistorySource, changeLogHistoryTarget, productLineConflictModel);
		
		
		
	}
	
	/**
	 * <p> Execute the information for the user and show it for the user in the GUI </p>
	 * 
	 * <p> Created at:  14/10/2013  </p>
	 *
	 * @param startTime
	 * @param changeLogHistorySource
	 * @param changeLogHistoryTarget
	 * @return
	 */
	protected void saveAndShowEvolutionResults(ChangeLogHistory changeLogHistorySource, ChangeLogHistory changeLogHistoryTarget, ConflictModel productLineConflictModel) {
		
		System.out.println("Saving direct conflict information in:  "+showEvolutionsUI.getWorkDirectoryPath());
		
		ChangeLogHistoryUtil.writeHistoryChangeLogFile(changeLogHistorySource, new File(showEvolutionsUI.getWorkDirectoryPath()+PluginConstants.HISTORY_CHANGE_LOG_SOURCE_FILE));
		ChangeLogHistoryUtil.writeHistoryChangeLogFile(changeLogHistoryTarget, new File(showEvolutionsUI.getWorkDirectoryPath()+PluginConstants.HISTORY_CHANGE_LOG_TARGET_FILE));
		//@deprecated ProductLineConflictModelUtil.writeProductLineConflictModelToFile(productLineConflictModel, new File(evolutionUI.getWorkDirectoryPath()+PluginConstants.PRODUCTI_LINE_CONFLICT_MODEL_FILE));
		
		
		showEvolutionsUI.showEvolutionsAnalisis(changeLogHistorySource, changeLogHistoryTarget, productLineConflictModel);
	}
	
	
}
